package com.ls.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.entity.ProductDTO;
import com.ls.mapper.ProductMapper;
import com.ls.pojo.Product;
import com.ls.service.IProductService;
import com.ls.util.PageData;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaotang
 * @since 2023-04-07
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Resource
    private ProductMapper productMapper;


    @Override
    public Integer updateStockById(Integer id) {
      return   productMapper.updateStockById(id);
    }

    @Override
    public PageData getPageData(Integer currentPage, Integer pageSize, ProductDTO productDTO) {

        // 1. 构建查询条件
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(productDTO.getProductName())) {
            queryWrapper.like(Product::getProductName, productDTO.getProductName());
        }

        if (!StringUtils.isEmpty(productDTO.getStatus())) {
            queryWrapper.eq(Product::getStatus, productDTO.getStatus());
        }


        // 2. 创建分页对象
        // 3. 调用mapper查询数据 这边调用service
        Page<Product> page = new Page<>(currentPage, pageSize);

        Page<Product> productPage = this.page(page, queryWrapper);
        // 4. 封装返回的对象 pageData
        PageData pageData = new PageData(productPage.getTotal(), productPage.getRecords());
        // 5. 返回数据
        return pageData;

    }
}
